package com.zx.tv.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.URLHelper;
import com.zx.base.util.DPIExchange;
import com.zx.base.util.UpdateVersion;
import com.zx.tv.R;
import com.zx.tv.app.NewTVApp;
import com.zx.tv.entity.UpdateInfo;

public class WeatherActivity extends Activity {

	private UpdateInfo up_info = new UpdateInfo();
	private UpdateVersion mUpdateVersion;
	private LinearLayout start_main;
	private ImageView weather_img;
	private TextView weather_city, weather_date, weather_weathers,
			weather_temp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		NewTVApp app=(NewTVApp)getApplication();
		setContentView(R.layout.weatherlayout);
		DPIExchange dpi = new DPIExchange(this);
		start_main = (LinearLayout) findViewById(R.id.start_main);
		LayoutParams lp = new LayoutParams(new LayoutParams(
				dpi.widthChange(700), dpi.heightChange(380)));
		lp.setMargins(dpi.widthChange(200), dpi.heightChange(300), 0, 0);
		start_main.setLayoutParams(lp);
		weather_city=(TextView)findViewById(R.id.weather_city);
		weather_date=(TextView)findViewById(R.id.weather_date);
		weather_weathers=(TextView)findViewById(R.id.weather_weathers);
		weather_temp=(TextView)findViewById(R.id.weather_temp);
		weather_img=(ImageView)findViewById(R.id.weather_img);
		
		weather_city.setText(app.weather.getCity());
		weather_date.setText(Html.fromHtml(app.weather.getDate()));
		weather_weathers.setText(app.weather.getWeathers());
		weather_temp.setText(app.weather.getTemp());
		int aa=(Integer) (app.weathermap.get(app.weather.getImg()));
		weather_img.setBackgroundResource(aa);
		new InfoTask().execute(URLHelper.mPrefix);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(WeatherActivity.this, Main.class);
		startActivity(intent);
		WeatherActivity.this.finish();

		return super.onKeyDown(keyCode, event);
	}

	class InfoTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "getNewVersion");
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息
		}

		@Override
		protected void onPostExecute(String result) {

			if (Constant.debug)
				Log.e("TAG", "version result = " + result);

			if (result == null || result.equals("")) {
				// 发送解析数据 消息

			} else if (result.equals("timeout")) {

			} else {
				// 发送解析数据 消息
				parseVersionJson(result);
			}
		}

		// 解析获取的服务器json数据
		private void parseVersionJson(String str) {
			try {
				String succeed = new JSONObject(str).getString("succeed");
				if (succeed.equals("000")) {

					String data = new JSONObject(str).getString("userData");

					if (Constant.debug)
						Log.e("TAG", "data = " + data);

					String apkurl = new JSONObject(data).getString("apkurl");
					up_info.setApkurl(apkurl == null ? "" : apkurl);
					String apkname = new JSONObject(data).getString("apkname");
					up_info.setApkname(apkname == null ? "" : apkname);
					String apkversion = new JSONObject(data)
							.getString("apkversion");
					up_info.setApkversion(apkversion == null ? "" : apkversion);
					String apkremark = new JSONObject(data)
							.getString("apkremark");
					up_info.setApkremark(apkremark == null ? "" : apkremark);
					String apksize = new JSONObject(data).getString("apksize");
					up_info.setApksize(apksize == null ? "" : apksize);

					checkupdate();
				} else {

				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	private void checkupdate() {

		if (!(up_info.getApkurl()).equals("")) {
			String url = URLHelper.main + up_info.getApkurl();
			double current_version = 1.0;
			double new_version = 1.0;

			try {
				current_version = Double.parseDouble(getVersionName());
				new_version = Double.parseDouble(up_info.getApkversion());
			} catch (Exception e) {
				Log.e("TAG", "版本异常");
			}

			if ((new_version > current_version)
					&& (!(up_info.getApkurl().equals("")))) {
				mUpdateVersion = new UpdateVersion(WeatherActivity.this, url,
						up_info.getApkremark());
				// mUpdateVersion.checkUpdateInfo();
				Dialog dialog = mUpdateVersion
						.buildDialog1(WeatherActivity.this);
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
			} else {

			}
		}

	}

	private String getVersionName() {
		String version_name = "";
		try {
			PackageManager packageManager = getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			version_name = packInfo.versionName;
		} catch (Exception e) {

		}
		return version_name;

	}

}
